import {defineStore} from 'pinia'
import { _login ,_getlist,_delete,_editlist, _addlist,_batchdeletion, _getSelectList} from '@/api'
import type { logintype } from '@/types/login'
import { dayjs } from 'element-plus'
import { citys } from '@/views/Children/LargeScreenManagement/Children/EmploymentSituation/city'
import { ElMessage } from 'element-plus'
export const useStore = defineStore('login',{
    state: () => {
  return{
        token:'',
            isCollapse:true,//控制Menu的显示与隐藏
            page:'1',//页数
            size:'10',//每页显示条数
            Alllist:<any[]>[],//总数据
            listlength:'',//总数据的长度
            list:[],//分页后获取到的数据
            market:<any[]>[],//市场部
            show:false,//控制模态框的显示与隐藏
            title:'',//控制模态框中的标题
            rowData:{
                id:'',
                stuNo:'',
                stuName:'',
                status:'',
                salary:'',
                workCity:'',
                collegeId:'',
                expertiseId:'',
                origin:'',
                className:'',
                createdTime:'',
                lastModTime:'',
                archived:'',
                gradYear:'',
                
              
            },
            pageCount:0,//控制分页的总页数
            str:"",
            workCitys:<any[]>[],
            ids:<any>[]
  }     
  },
    actions:{
        // 登录
        async _loginActions(params:logintype){
            const {data} = await _login(params)
            if(data.code){
                this.token = data.token;
                localStorage.setItem('token',data.token)
                this.getlist()
                ElMessage.success(data.message)
                setTimeout(()=>{
                      location.href='/home/HomePage'
                },300)
              
            } else {
                ElMessage.error(data.message)
            }
        },
        // 修改图标的显示隐藏
        ChangisCollapse(){
            this.isCollapse = !this.isCollapse
        },
        // 获取数据
        async getlist(){
                const params = {
                     page:this.page,
                     size:this.size
                }
                const {data:resultdata} = await _getlist(params)
                if(resultdata.code){
                    this.listlength = resultdata.lengths
                    this.list = resultdata.result
                    this.Alllist = resultdata.Alllist
                    this.pageCount =   resultdata.lengths / Number(this.size)
                    this.Alllist.forEach((item:any)=>{
                        if(typeof item.origin !==  'undefined'){
                            return this.market.push(item.origin) 
                        }
                        
                    })
                    // 数组去重
                    this.unique(this.market)
                    // 处理状态
                    this.useUtils(this.list)
                  
                    // 处理学院ID
                    this.useCollegeId(this.list)

                    // 处理时间
                    // this.usetime(this.list)
                    // 处理城市
                    this.useCityWord(this.list)
                    // 处理专业
                    this.useExpertiseld(this.list)
                    // 处理毕业学校
                    this.useArchived(this.list)
                }
        },  
        // 数组去重
        unique(arr:any){            
            for(let i=0; i<arr.length; i++){
            for(let j=i+1; j<arr.length; j++){
                if(arr[i]==arr[j]){        
                    arr.splice(j,1);
                    j--;
                } 
            }
        }
            return arr;
        },
        // 处理status
        useUtils(arr:any){
              arr.forEach((item:any) => {
                if(item.status == '1'){
                    item.status = '未毕业'
                } else if(item.status == '2'){
                    item.status = '返校重听'
                } else if(item.status == '3'){
                    item.status = '已就业'
                } else if(item.status == '4'){
                    item.status = '返回未就业'
                } else if(item.status == '5'){
                    item.status = '以往未就业'
                }
                    });
                return arr
        },
        // 处理大学ID
        useCollegeId(arr:any){
                 arr.forEach((item:any) => {
                            if(item.collegeId == '1'){
                                item.collegeId = '游戏学院'
                            } else if(item.collegeId == '2'){
                                item.collegeId = '网站学院'
                            } 
                    });
                return arr
        },
        // 处理城市词
        useCityWord(arr:any){
        arr.forEach((item:any)=>{
            citys.forEach((v:any)=>{
                if(item.workCity.length == 4){
                    
                if(item.workCity == v.value.substring(0,4)){
                        item.workCitys = v.label
                    } else {
                         v.children.forEach((val:any)=>{
                            if(val.value.substring(0,4) == item.workCity){
                                item.workCitys = val.label
                            }
                        })
                    }

                } else if(item.workCity.length == 18 ){
                    this.workCitys.push(item.workCity.substring(0,6))
                    this.workCitys.push(item.workCity.substring(6,12))
                    this.workCitys.push(item.workCity.substring(12,18))
                     item.workCity = this.workCitys
                     this.workCitys = []                    
                    item.workCity.forEach((items: any)=>{
                        if(items == v.value){
                            this.str+=v.label +'/'
                        }
                        v.children.forEach((itemss:any)=>{
                            if(itemss.value == items){
                                this.str += itemss.label +'/'
                            }
                            itemss.children.forEach((itemsss:any)=>{
                                if(itemsss.value == items){
                                    this.str += itemsss.label 
                                    item.workCitys = this.str
                                this.str = ""
                                }
                                
                            })
                        })
                         
                    })

                   
                } else if(Array.isArray(item.workCity)){
                     item.workCity.forEach((items: any)=>{
                        if(items == v.value){
                            this.str+=v.label +'/'
                        }
                        v.children.forEach((itemss:any)=>{
                            if(itemss.value == items){
                                this.str += itemss.label +'/'
                            }
                            itemss.children.forEach((itemsss:any)=>{
                                if(itemsss.value == items){
                                    this.str += itemsss.label 
                                    item.workCitys = this.str
                                    this.str = ""
                                }
                                
                            })
                        })
                         
                    })
                }
                    
                })
            })
            return arr
        },
        // 处理专业
        useExpertiseld(arr:any){
            arr.forEach((item:any)=>{
                if(item.expertiseId == '1'){
                    item.expertiseId = '游戏设计'
                } else if(item.expertiseId == '2'){
                    item.expertiseId = '游戏运营'
                } else if(item.expertiseId == '3'){
                    item.expertiseId = '元宇宙'
            }})
            return arr
        },
        // 处理毕业学校
        useArchived(arr:any){
            arr.forEach((item:any)=>{
                if(item.archived == '1'){
                    item.archived = '八维大学'
                }
            })
            return arr
        },
        // 调用删除接口
        async _deleteActions(id:string){
            const {data} = await _delete(id)
            if(data.code){
                  ElMessage({
                    message: data.message,
                    type:"success"
                  })
            }else{
                ElMessage({
                    message: data.message,
                    type: 'warning',
                })
            }
            
        },
        // 获取到的数据复制到rowData中
        async _getidlistActions(row:any){
            this.rowData = row
        },
        // 修改show
        ChangeShow(){
            this.show = !this.show
        },
        // 修改title
        ChangeTitle(title:string){
            this.title = title
        },
        // 修改页数
        ChangePageActions(page:string){
            this.page = page
        },
        // 修改每页显示条数
        ChangeSizeActions(value:string){
            this.size = value
        },
        // 处理rowData
        UseRowData(rowData:any){
            // 处理状态
            if(rowData.status == '未毕业'){
                rowData.status = '1'
            } else if(rowData.status == '返校重听'){
                rowData.status = '2'
            } else if(rowData.status == '已就业'){
                rowData.status = '3'
            } else if(rowData.status == '返回未就业'){ 
                rowData.status = '4'
            } else if(rowData.status == '以往未就业'){
                rowData.status = '5'
            }
            // 处理学院
            if(rowData.collegeId == '游戏学院'){
                rowData.collegeId = '1'
            } else if(rowData.collegeId == '网站学院'){
                rowData.collegeId = '2'
            }
            // 处理专业
            if(rowData.expertiseId == '游戏设计'){
                rowData.expertiseId = '1'
            } else if(rowData.expertiseId == '游戏运营'){
                rowData.expertiseId = '2'
            } else if(rowData.expertiseId == '元宇宙'){
                rowData.expertiseId = '3'
            }
            // 处理毕业学校
            if(rowData.archived == '八维大学'){
                rowData.archived = '1'
            }
            // 处理时间
            // 处理城市编码
            this.rowData.workCity.forEach((item:any) => {
            this.str += item
            });
            this.rowData.workCity = this.str
            this.str = ""
             return rowData
        },
        // 修改数据的方法
        async _editlistActions(){
            this.UseRowData(this.rowData)
            const {data} = await _editlist(this.rowData)
            if(data.code){
                  ElMessage({
                    message: data.message,
                    type:"success"
            })
            } else{
                ElMessage({
                    message: data.message,
                    type:"error"
            })
            }
            this.ResetRowData()
        },
        // 还原rowData
        ResetRowData(){
            this.rowData = {
                id:'',
                stuNo:'',
                stuName:'',
                status:'',
                salary:'',
                workCity:'',
                collegeId:'',
                expertiseId:'',
                origin:'',
                className:'',
                createdTime:'',
                lastModTime:'',
                archived:'',
                gradYear:''
            }
        },
        // 添加的方法
        async _addlistActions(){
            console.log('111');
            this.rowData.createdTime = dayjs().format('YYYY-MM-DD:HH:mm:ss')
            this.rowData.lastModTime = dayjs().format('YYYY-MM-DD:HH:mm:ss')
            this.rowData.archived = '1'
            this.UseRowData(this.rowData)
            console.log(this.rowData,'this.rowData');
            const {data} = await _addlist(this.rowData)
            console.log(data,'data');
            if(data.code){
                  ElMessage({
                    message: data.message,
                    type:"success" 
                  })
            } else {
            ElMessage({
                    message: data.message,
                    type:"error"
                })
            }
            this.show = false
            this.ResetRowData()
        
        },
        //根据id进行批量删除
        async _batchdeleteActions(){
            const {data} = await _batchdeletion(this.ids)
            console.log(data);
            if(data.code){
                ElMessage({
                    message: data.message,
                    type:"success" 
                  })
                this.ids = []
            } else {
                 ElMessage({
                    message: data.message,
                    type:"error" 
                  })
                this.ids = []
            }
        },
        // 查询
        async _selectActions(params:object){
            const {data} = await _getSelectList(params)
            console.log(data);
            if(data.code){
                this.list = data.result
            }
              // 处理状态
                    this.useUtils(this.list)
                  
                    // 处理学院ID
                    this.useCollegeId(this.list)

                    // 处理时间
                    // this.usetime(this.list)
                    // 处理城市
                    this.useCityWord(this.list)
                    // 处理专业
                    this.useExpertiseld(this.list)
                    // 处理毕业学校
                    this.useArchived(this.list)
        }
    },
    // 实现数据持久化
    persist:true
})

                